package com.zxl.manager.privacy.utils.utils;

/**
 * 温度<br>
 */
public class Temperature {

	private float mValue;
	private TemperatureUnit mUnit;

	public Temperature(float value, TemperatureUnit unit) {
		mValue = value;
		mUnit = unit;
	}

	/**
	 * 设置温度数值<br>
	 * 
	 * @param value
	 */
	public void setValue(float value) {
		mValue = value;
	}

	/**
	 * 获取温度数值<br>
	 * 
	 * @return
	 */
	public float getValue() {
		return mValue;
	}

	/**
	 * 获取整型的温度数值<br>
	 * 
	 * @return
	 */
	public int getIntValue() {
		return (int) mValue;
	}

	/**
	 * 获取温度单位<br>
	 * 
	 * @return
	 */
	public TemperatureUnit getUnit() {
		return mUnit;
	}

	/**
	 * 获取常规格式化的显示温度字符串, 形如20°C<br>
	 */
	public String getFormatString() {
		return getIntValue() + mUnit.getSymbol();
	}

	/**
	 * 获取简化形式的温度字符串，形如20°<br>
	 */
	public String getSimpleString() {
		return getIntValue() + mUnit.getSimpleSymbol();
	}

	/**
	 * 转换为摄氏温度<br>
	 */
	public Temperature toCelsius() {
		if (mUnit == TemperatureUnit.Celsius) {
			return this;
		}
		if (mUnit == TemperatureUnit.Fahrenheit) {
			mValue = (mValue - 32) / 1.8f;
		} else if (mUnit == TemperatureUnit.Kelvin) {
			mValue = mValue - 273.15f;
		} else {
			throw new IllegalStateException("new unit?");
		}
		mUnit = TemperatureUnit.Celsius;
		return this;
	}

	/**
	 * 转换为华氏温度<br>
	 */
	public Temperature toFahrenheit() {
		if (mUnit == TemperatureUnit.Fahrenheit) {
			return this;
		}
		toCelsius();
		mValue = mValue * 1.8f + 32;
		mUnit = TemperatureUnit.Fahrenheit;
		return this;
	}

	/**
	 * 转换为开尔文温度<br>
	 */
	public Temperature toKelvin() {
		if (mUnit == TemperatureUnit.Kelvin) {
			return this;
		}
		toCelsius();
		mValue = mValue + 273.15f;
		mUnit = TemperatureUnit.Kelvin;
		return this;
	}

	/**
	 * 转换到指定的温度单位, 会引起数值相应地转换<br>
	 * 
	 * @param unit
	 */
	public Temperature toTemperatureUnit(TemperatureUnit unit) {
		if (unit == null) {
			return null;
		}
		if (TemperatureUnit.Celsius.equals(unit)) {
			return toCelsius();
		} else if (TemperatureUnit.Fahrenheit.equals(unit)) {
			return toFahrenheit();
		} else if (TemperatureUnit.Kelvin.equals(unit)) {
			return toKelvin();
		} else {
			throw new IllegalStateException("new unit?");
		}
	}
}
